import { Product } from "../models/product"

export const GET_PRODUCT = 'GET_PRODUCT'
export const GET_PRODUCT_SUCCESS = 'GET_PRODUCT_SUCCESS'

type sortBy =  'createdAt'| 'sold'

export interface GetProductAction {
  type: typeof GET_PRODUCT,
  sortBy: sortBy,
  order: string,
  limit: number
}

export interface GetPorductSuccessAction {
  type: typeof GET_PRODUCT_SUCCESS,
  payload: Product[],
  sortBy: string
}

export const getProduct = (
  sortBy: sortBy,
  order: string = 'desc',
  limit: number = 10
  ):GetProductAction=> ({
  type: GET_PRODUCT,
  sortBy,
  order,
  limit
})

export const getProductSuccess = (payload:Product[],sortBy:string):GetPorductSuccessAction=>({
  type: GET_PRODUCT_SUCCESS,
  payload,
  sortBy
})

/**
 * 搜索商品
 */

export const SEARCH_PRODUCT = 'SEARCH_PRODUCT'
export const SEARCH_PRODUCT_SUCCESS = 'SEARCH_PRODUCT_SUCCESS'


export interface SearchProducPayload {
  category: string,
  search: string
}

export interface SearchProductAction {
  type: typeof SEARCH_PRODUCT,
  payload: SearchProducPayload
}

export interface SearchProductSuccessAction {
  type: typeof SEARCH_PRODUCT_SUCCESS,
  products: Product[]
}

export const searchProduct = (payload: SearchProducPayload):SearchProductAction => ({
  type: SEARCH_PRODUCT,
  payload
})

export const searchProductSuccess = (products:Product[]):SearchProductSuccessAction => ({
  type: SEARCH_PRODUCT_SUCCESS,
  products
})

/**
 * 筛选相关
 */

export const FILTER_PRODUCT = 'FILTER_PRODUCT'
export const FILTER_PRODUCT_SUCCESS = 'FILTER_PRODUCT_SUCCESS'

export interface FilterPayload {
  order?: string,
  sortBy?: string,
  limit?: number,
  skip: number,
  filters?: {
    category: string[],
    price: number[]
  }
}

export interface FilterProductAction {
  type: typeof FILTER_PRODUCT,
  payload: FilterPayload
}

export interface FilterProductSuccessAction {
  type: typeof FILTER_PRODUCT_SUCCESS,
  payload: {
    size: number,
    data: Product[]
  },
  skip: number
}

export const filterProduct = (payload:FilterPayload):FilterProductAction =>({
  type: FILTER_PRODUCT,
  payload
})

export const filterProductSuccess = (
  payload: {
    size: number,
    data: Product[]
  },
  skip: number
):FilterProductSuccessAction =>({
  type: FILTER_PRODUCT_SUCCESS,
  payload,
  skip
})

/**
 * 获取产品详情
 */

export const GET_PRODUCT_BY_ID = 'GET_PRODUCT_BY_ID'
export const GET_PRODUCT_BY_ID_SUCCESS = 'GET_PRODUCT_BY_ID_SUCCESS'

export interface GetProductByIdAciton {
  type: typeof GET_PRODUCT_BY_ID,
  payload: {
    productId: string
  }
}

export interface GetProductByIdSuccessAciton {
  type: typeof GET_PRODUCT_BY_ID_SUCCESS,
  payload: Product
}

export const getProductById = (payload:{ productId: string }):GetProductByIdAciton=>({
  type: GET_PRODUCT_BY_ID,
  payload
})

export const getProductByIdSuccess = (payload: Product):GetProductByIdSuccessAciton=>({
  type: GET_PRODUCT_BY_ID_SUCCESS,
  payload
})

export type ProductUnionType = 
| GetProductAction
| GetPorductSuccessAction
| SearchProductAction
| SearchProductSuccessAction
| FilterProductAction
| FilterProductSuccessAction
| GetProductByIdAciton
| GetProductByIdSuccessAciton